package com.mt.mtyx.acl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.mtyx.acl.service.AdminRoleService;
import com.mt.mtyx.acl.service.AdminService;
import com.mt.mtyx.acl.service.RoleService;
import com.mt.mtyx.common.exception.BusinessException;
import com.mt.mtyx.common.result.Result;
import com.mt.mtyx.entity.acl.Admin;
import com.mt.mtyx.vo.acl.AdminQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * 用户管理控制层
 *
 * @author WMT
 * @Date 2023/6/25 20:44
 */
@Api(tags = "用户管理")
@RequestMapping("/admin/acl/user")
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private AdminRoleService adminRoleService;

    @Autowired
    private RoleService roleService;


    @ApiOperation("分页条件查询用户列表")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<Admin>> adminList(
            @PathVariable Long page,
            @PathVariable Long limit,
            AdminQueryVo adminQueryVo
    ) throws Exception {
        // 1.封装分页条件
        Page<Admin> rolePage = new Page<>(page, limit);
        // 2.调用分页查询方法
        return Result.success(adminService.getAdminList(rolePage, adminQueryVo));
    }

    @ApiOperation("根据用户id查询详情")
    @GetMapping("/get/{id}")
    public Result<Admin> selectById(@PathVariable Long id) throws Exception {
        return Result.success(adminService.getById(id));
    }

    @ApiOperation("添加用户")
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody Admin admin) throws Exception {
        if (StringUtils.isEmpty(admin.getUsername()) || StringUtils.isEmpty(admin.getPassword())) {
            throw new BusinessException("用户名或密码不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (adminService.countByUsername(admin.getUsername()) > 0) {
            throw new BusinessException("用户名已存在！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(adminService.insert(admin));
    }

    @ApiOperation("根据id删除用户")
    @DeleteMapping("/remove/{id}")
    public Result<Integer> remove(@PathVariable Long id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(adminService.delete(id));
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public Result<Integer> batchRemove(@RequestBody List<Long> ids) throws Exception {
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(adminService.batchRemove(ids));
    }

    @ApiOperation("修改用户")
    @PutMapping("/update")
    public Result<Integer> update(@RequestBody Admin admin) throws Exception {
        if (StringUtils.isEmpty(admin.getId())) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (StringUtils.isEmpty(admin.getUsername())) {
            throw new BusinessException("用户名不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        Admin adminDb = adminService.getById(admin.getId());
        if (adminDb != null) {
            if (!adminDb.getUsername().equals(admin.getUsername())) {
                if (adminService.countByUsername(admin.getUsername()) > 0) {
                    throw new BusinessException("用户名已存在！", HttpStatus.BAD_REQUEST.value());
                }
            }
        }
        return Result.success(adminService.update(admin));
    }


    @ApiOperation("根据用户id查询角色")
    @GetMapping("/toAssign/{adminId}")
    public Result<Map<String,Object>> toAssign(@PathVariable Long adminId) throws Exception {
        if (StringUtils.isEmpty(adminId)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(roleService.getRoleByUserId(adminId));
    }

    @ApiOperation("根据用户id添加角色")
    @PostMapping("/doAssign")
    public Result<Integer> doAssign(
            @RequestParam("adminId") Long adminId,
            @RequestParam("roleIds") List<Long> roleIds
    ) throws Exception {
        if (StringUtils.isEmpty(adminId)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (roleIds == null || roleIds.isEmpty()) {
            throw new BusinessException("角色id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(roleService.saveAdminRole(adminId, roleIds));
    }
}
